<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Lib\Params\BaseWebParams;
use App\Lib\Params\InfoSearchParams;
use App\Lib\Params\WebColumnSearchParams;
use App\Model\Content;
use App\Model\Info;
use App\Model\InfoExtField;
use App\Model\InfoExtRelate;
use App\Model\WebColumn;
use Illuminate\Support\Facades\Input;
use function var_dump;

class InfoController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//d

	}

    public function search()
    {        $searchData = Input::get('searchData',array());


        $infoSearch = new \App\Lib\Params\InfoSearchParams($searchData);
        $infos = Info::search($infoSearch);

        $webColumnSearch = new WebColumnSearchParams(array('parentId'=>1));
        $firstWebColumns = WebColumn::search($webColumnSearch);
        $secondWebColumns = array();

        if($infoSearch->firstColumnId > 0){
            $webColumnSearch->setParentId($infoSearch->firstColumnId);
            $secondWebColumns = WebColumn::search($webColumnSearch);
        }
        return View('infoList')
            ->with('infoSearch',$infoSearch)
            ->with('infos',$infos)
            ->with('firstWebColumns',$firstWebColumns)->with('secondWebColumns',$secondWebColumns);
    }

    public function info($infoId=0)
    {
        $webColumnSearch = new WebColumnSearchParams(array('parentId'=>1));
        $firstWebColumns = WebColumn::search($webColumnSearch);
        $secondWebColumns = array();
        $extFields = InfoExtField::getExtFields();
        if(0 !== $infoId){
            $info = Info::getInfoById($infoId);
            if(is_null($info)){
                //TODO
            }
            if(isset($info->first_column_id) && $info->first_column_id > 0){
                $webColumnSearch->setParentId($info->first_column_id);
                $secondWebColumns = WebColumn::search($webColumnSearch);
            }
            $extRelates = InfoExtRelate::getExtRelatesByInfoId($info->id);
        }else{
            $info = null;
            $extRelates = array();
        }

        return View('info')
            ->with('info',$info)
            ->with('extFields',$extFields)
            ->with('extRelates',$extRelates)
            ->with('firstWebColumns',$firstWebColumns)->with('secondWebColumns',$secondWebColumns);
    }

    public function content($infoId=0,$pageNo=1)
    {
        $info = Info::getInfoById($infoId,array('id','title'));
        if(is_null($info)){
            //TODO
        }

        $content = Content::getContentByInfoIdAndPageNo($infoId,$pageNo,array('id','page_no','content'));

        return View('content')
            ->with('info',$info)
            ->with('content',$content);
    }

    public function infoSave()
    {
        $infoInput = Input::get('infoData',null);
        if(is_null($infoInput)){

        }
        $operatorId = BaseWebParams::getAdminId();
        if(is_numeric($infoInput['pagecount']) && $infoInput['pagecount'] > 1){
            $infoInput['is_page'] = 1;
        }else{
            $infoInput['is_page'] = 0;
            $infoInput['pagecount'] = 1;
        }
        $info = array('title'=>$infoInput['title'],'keywords'=>$infoInput['keywords'],'description'=>$infoInput['description'],'index_main_img'=>$infoInput['index_main_img'],'main_img'=>$infoInput['main_img'],'intro'=>$infoInput['intro'],'first_column_id'=>$infoInput['firstColumnId'],'column_id'=>$infoInput['secondColumnId'],'is_page'=>$infoInput['is_page'],'pagecount'=>$infoInput['pagecount'],'business_id'=>BaseWebParams::getBusinessId(),'order_id'=>$infoInput['order_id'],'operator_id'=>$operatorId);

        $extInfo = array();
        if(isset($infoInput['ext']) && !empty($infoInput['ext'])){
            $extFieldsData = InfoExtField::getExtFieldsData();
            foreach($infoInput['ext'] as $extField=>$extRelate){
                if(is_null($extRelate)){
                    continue;
                }
                $extTemp = array();
                if(!is_null($infoInput['id'])){
                    $extTemp['info_id'] = $infoInput['id'];
                }
                if(isset($extFieldsData[$extField])){
                    $extTemp['field_id'] = $extFieldsData[$extField];
                }
                $extTemp['field_value'] = $extRelate;
                $extInfo[] = $extTemp;
            }
        }
        $result = false;
        if(!isset($infoInput['id'])){
            $infoId = Info::addInfo($info);
            if($infoId <= 0){
                echo '失败';
                exit;
            }
            foreach($extInfo as $extKey=>$ext){
                $extInfo[$extKey]['info_id'] = $infoId;
            }
        }else{
            $result = Info::updateInfoById($infoInput['id'],$info);
        }
        if(!empty($extInfo)){
            InfoExtRelate::deleteExtRelates($infoInput['id']);
            $result = InfoExtRelate::addExtRelates($extInfo);
        }
        if(false === $result){
            echo '失败';
        } else{
            echo '成功';
        }
    }

    public function contentSave()
    {
        $infoInput = Input::get('Info',null);
        if(is_null($infoInput)){

        }

        $info = Info::getInfoById($infoInput['infoId'],'id');
        if(is_null($info)){
            //TODO
        }

        $operatorId = 1;
        $content = array('info_id'=>$infoInput['infoId'],'content'=>htmlentities($infoInput['content']),'page_no'=>$infoInput['page_no'],'operator_id'=>$operatorId);

        $result = false;
        if(0 == $infoInput['id']){
            $result = Content::addContent($content);
        }else{
            $result = Content::updateContentByInfoIdAndPageNo($infoInput['infoId'],$infoInput['page_no'],$content);
        }
        if(false === $result){
            echo '失败';
        } else{
            echo '成功';
        }
    }

    public function delete($infoId)
    {
        $result = Info::deleteInfo($infoId);
        if(false === $result){
            echo '失败';
        } else{
            echo '成功';
        }
    }
	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
